﻿<%@ WebHandler Language="C#" Class="DLLAjaxHandler" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.SessionState;
using TaoSH.AppCode;
using System.Text;
using TaoSH.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

/// <summary>
/// Summary description for LoginHandler
/// </summary>
public class DLLAjaxHandler : IHttpHandler, IRequiresSessionState
{
    private Object thisLock = new Object();
    public void ProcessRequest(HttpContext context)
    {
        context.Response.Buffer = true;
        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";
        context.Response.ContentType = "text/plain";

        string strAction = context.Request.QueryString["Action"] as string;
        if (string.IsNullOrEmpty(strAction))
        {
            strAction = context.Request.Form["Action"] as string;
        }

        string strUserName = HttpContext.Current.Session["UserName"] as string;
        if (string.IsNullOrEmpty(strUserName))
        {
            strUserName = "";
        }
        try
        {

            switch (strAction)
            {
                case "GetLeiBie":
                    {

                        List<PT_Pro_Category> ListLB = new PT_Pro_CategoryB().GetEntities(d => d.TopID.ToString() == "0").ToList();
                        context.Response.Write(JsonConvert.SerializeObject(ListLB));
                    }
                    break;
                case "GetZLeiBie":
                    {
                        string strPID = context.Request["ID"] ?? "";
                        List<PT_Pro_Category> ListLB = new PT_Pro_CategoryB().GetEntities(d => d.TopID.ToString() == strPID).ToList();
                        context.Response.Write(JsonConvert.SerializeObject(ListLB));
                    }
                    break;
                case "Login":
                    {
                        string strUser = context.Request["UserName"] ?? "";
                        string strPAD = context.Request["PAD"] ?? "";
                        JH_Auth_UserB BUser = new JH_Auth_UserB();
                        string strReturn = "";
                        if (strUser == "" || strPAD == "")
                        {
                            strReturn = "用户名和密码不能为空";
                        }
                        if (BUser.GetEntity(d => d.UserName == strUser && d.UserPass == strPAD && d.IsUse == "Y") != null)
                        {
                        }
                        else
                        {
                            strReturn = "用户名或密码错误";
                        }
                        context.Response.Write(strReturn);

                    }
                    break;
                case "UpdateTJ"://更新推荐状态
                    {
                        string strUser = context.Request["Status"] ?? "N";
                        string strID = context.Request["ID"] ?? "";
                        new PT_Pro_ProductB().ExsSclarSql(" UPDATE  PT_Pro_Product SET ISTJ='" + strUser + "' , TJTime='" + DateTime.Now + "' WHERE ID='" + strID + "'");
                        context.Response.Write("");

                    }
                    break;
                case "GetTJ"://获取推荐商品
                    {
                        string euser = context.Request["euser"];
                        string strLB1 = context.Server.UrlDecode(context.Request["LB1"]) ?? "";
                        int CurPage = int.Parse(context.Request["Page"] ?? "1");
                        string strWhere = "ISTJ = 'Y'";
                        if (strLB1 != "")
                        {
                            strWhere = strWhere + "AND Category1='" + strLB1 + "'";
                        }
                        if (string.IsNullOrEmpty(euser))
                        {
                            euser = "admin";
                        }
                        strWhere += " AND CreateUser='" + euser + "'";

                        List<PT_Pro_Product> ListLB = new PT_Pro_ProductB().GetEntities(strWhere).OrderByDescending(D => D.TJTime).Take(9 * CurPage).Skip(9 * (CurPage - 1)).ToList();
                        context.Response.Write(JsonConvert.SerializeObject(ListLB));

                    }
                    break;
                case "GetLBSX"://获取类别属性
                    {
                        string strLB1 = context.Server.UrlDecode(context.Request["LB1"] ?? "");
                        string strLB2 = context.Server.UrlDecode(context.Request["LB2"] ?? "");
                        List<PT_Pro_Category_SX> ListLB = new PT_Pro_Category_SXB().GetEntities(d => d.LB1 == strLB1 && d.LB2 == strLB2).ToList();
                        context.Response.Write(JsonConvert.SerializeObject(ListLB));
                    }
                    break;
                case "GetSPXQ"://获取商品详情
                    {
                        string strSPID = context.Request["SPID"] ?? "";
                        List<PT_Pro_Product> ListLB = new PT_Pro_ProductB().GetEntities(d => d.ID.ToString() == strSPID).ToList();
                        context.Response.Write(JsonConvert.SerializeObject(ListLB));
                    }
                    break;
                case "GetGG"://获取广告
                    {
                        string euser = context.Request["euser"];
                        string strLB = context.Server.UrlDecode(context.Request["LeiBie"] ?? "");
                        if (string.IsNullOrEmpty(euser))
                        {
                            euser = "admin";
                        }

                        List<PT_Info_Advert> ListLB = new PT_Info_AdvertB().GetEntities(d => d.Type == strLB && d.CreateUser == euser).ToList();
                        context.Response.Write(JsonConvert.SerializeObject(ListLB));
                    }
                    break;
                case "GetSPListBySX"://获取商品通过类别属性
                    {
                        JObject Retjson = new JObject();

                        string strReturn = "";
                        string euser = context.Request["euser"];
                        if (string.IsNullOrEmpty(euser))
                        {
                            euser = "admin";
                        }
                        
                        string strJsonData = context.Server.UrlDecode(context.Request["Data"] ?? "");
                        if (strJsonData != "")
                        {
                            JObject ja = (JObject)JsonConvert.DeserializeObject(strJsonData);

                            string cat1 = ja["cat1"].ToString();
                            string cat2 = ja["cat2"].ToString();
                            int Page = int.Parse(ja["Page"].ToString());
                            string Sort = ja["Sort"].ToString();
                            string catlist = ja["catlist"].ToString();
                            JArray jasxarr = (JArray)JsonConvert.DeserializeObject(catlist);
                            if (jasxarr.Count == 0)
                            {
                                //没有商品属性时查询到的商品
                                string strWhere = "Category1='" + cat1 + "' AND Category2='" + cat2 + "' and CreateUser='" + euser + "' ";
                                List<PT_Pro_Product> ListLB = new List<PT_Pro_Product>();
                                if (Sort == "1")
                                {
                                    ListLB = new PT_Pro_ProductB().GetEntities(strWhere).OrderBy(d => d.Price2).Take(9 * Page).Skip(9 * (Page - 1)).ToList();
                                }
                                else
                                {
                                    ListLB = new PT_Pro_ProductB().GetEntities(strWhere).OrderByDescending(d => d.Price2).Take(9 * Page).Skip(9 * (Page - 1)).ToList();
                                }
                                Retjson["COUNT"] = new PT_Pro_ProductB().GetEntities(strWhere).Count();

                                strReturn = JsonConvert.SerializeObject(ListLB);
                            }
                            else
                            {
                                string strAllSQL = "SELECT PT_Pro_Product.* FROM PT_Pro_Product_SX INNER JOIN PT_Pro_Product ON PT_Pro_Product_SX.SPID=PT_Pro_Product.ID WHERE PT_Pro_Product.Category1='" + cat1 + "' and PT_Pro_Product.Category2='" + cat2 + "' and PT_Pro_Product.CreateUser='" + euser + "' ";
                                string strSXWhere = "";
                                foreach (JObject itemsx in jasxarr)
                                {
                                    string stext = itemsx["stext"].ToString();
                                    string svalue = itemsx["svalue"].ToString();
                                    strSXWhere = strSXWhere + " AND PT_Pro_Product_SX.SXName='" + stext + "' AND PT_Pro_Product_SX.SXValue='" + svalue + "'";
                                }
                                DataTable dtResult = new PT_Pro_ProductB().GetDTByCommand(strAllSQL + strSXWhere + " ORDER BY Price2 " + (Sort == "1" ? "" : "DESC"));
                                Retjson["COUNT"] = dtResult.Rows.Count;

                                dtResult = dtResult.DTToPage(Page, 9);
                                strReturn = JsonConvert.SerializeObject(dtResult);
                            }
                            Retjson["Data"] = strReturn;
                            string json = Retjson.ToString();
                            context.Response.Write(json);

                        }

                    }
                    break;
                case "GetProcuteDetail":
                    {
                        string strId = context.Request["SPID"] ?? "";
                        string strProSQL = "SELECT Category1,Category2,Title,ImgUrl,ImgUrl1,Price1,Price2 FROM   PT_Pro_Product   WHERE  Id in (" + strId + ")";
                        DataTable dtProDetail = new PT_Pro_ProductB().GetDTByCommand(strProSQL);
                        string strProDetail = JsonConvert.SerializeObject(dtProDetail);
                        context.Response.Write(strProDetail);
                    }
                    break;
                case "AddNewOrder":
                    {
                        string euser = context.Request["euser"];
                        if (string.IsNullOrEmpty(euser))
                        {
                            euser = "admin";
                        }
                        string procuteDetail = context.Request.QueryString["proDetail"];
                        List<Procute> procuteList = JsonConvert.DeserializeObject<List<Procute>>(procuteDetail);
                        decimal sumPrice = 0;
                        string strProSQL = "";
                        string orderNo = "";
                        lock (thisLock)
                        {
                            orderNo = DateTime.Now.ToString("yyyyMMddHHmmss");
                        }
                        foreach (Procute p in procuteList)
                        {
                            strProSQL += string.Format(" insert into  PT_Order_Item (OrderID,PID,Title,ImgUrl,Price1,Price2,Price3,Qty,CreateUser,CreateTime)  SELECT '{0}',Id,Title,ImgUrl1,Price1,Price2,Price3,{1},{3},getDate() FROM   PT_Pro_Product WHERE Id ={2} ", orderNo, p.num, p.id, euser);
                            decimal price = new PT_Pro_ProductB().GetEntity(proc => proc.ID == p.id).Price2.Value;
                            sumPrice += price * p.num;
                        }
                        string strOrderSQL = string.Format("insert into PT_Order_Header (orderId,SumPrice,CreateUser,CreateTime) values('{0}',{1},{2},getdate()) ", orderNo, sumPrice, euser);
                        new PT_Pro_ProductB().ExsSql(strOrderSQL + strProSQL);
                        context.Response.Write("{\"orderNo\":\"" + orderNo + "\",\"sumPrice\":" + sumPrice + "}");
                    }
                    break;
                case "SearchProcute":
                    {
                        string key = context.Request["key"] ?? "";
                        string sort = context.Request["sort"] ?? "";
                        var list = new PT_Pro_ProductB().GetEntities(string.Format(" title like '%{0}%' ", key));
                        if (sort == "1")
                        {
                            list = list.OrderBy(p => p.Price2);
                        }
                        else
                        {
                            list = list.OrderByDescending(p => p.Price2);
                        }

                        context.Response.Write(JsonConvert.SerializeObject(list.ToList()));
                    }
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {

            context.Response.Write("error");
        }



    }

    public class Procute
    {
        public int id
        {
            get;
            set;
        }
        public int num
        {
            get;
            set;
        }

    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}